package com.allwees.bs.c.module.ops.controller;

import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.ops.req.SubscriptionReq;
import com.allwees.bs.c.module.ops.service.ISubscribeService;
import com.allwees.bs.core.modelbase.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 邮件订阅相关
 */
@RestController
@RequestMapping("/api/v1/edm")
public class SubscribeController extends BaseController {

	private static final String SECRET_QUICK_KEY = "1d7c35cb054c11ea8d4e0242ac110007";

    @Autowired
    private ISubscribeService subscribeService;

    /**
     * 邮件订阅
     * @param req
     * @return
     */
    @PostMapping("/subscribe")
    public R subscribe(@RequestBody SubscriptionReq req) {
        subscribeService.subscribe(req.getEmail(), getUserUuid());
        return R.ok();
    }

    /**
     * 取消邮件订阅
     * @param req
     * @return
     */
    @PostMapping("cancel-subscribe")
    public R cancelSubscribe(@RequestBody SubscriptionReq req) {
        subscribeService.cancelSubscribe(req.getEmail());
        return R.ok();
    }

    /**
     * 邮件快速订阅（用于email内部）
     * @param email
     * @param secretKey
     * @return
     */
    @GetMapping("/quick_subscribe")
    public R quickSubscribe(String email, String secretKey) {
    	if(SECRET_QUICK_KEY.equals(secretKey)) {
            subscribeService.subscribe(email, null);
            return R.ok();
    	} else {
            return R.error("Invalid secret key");
    	}
    }
}
